package com.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.db.domain.Brand;
import com.mall.db.service.BrandService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wxd
 * @since 2021-09-23
 */
@Controller
@RequestMapping("/brand/bc")
public class BrandController extends BaseController<Brand> {

    @Resource(name = "brandService")
    private BrandService bs;

    @Override
    public Map<String, Object> find(Brand brand) {

        //实现分页
        PageHelper.startPage(brand.getPage(),brand.getLimit());

        QueryWrapper<Brand> qw = new QueryWrapper<>();

        //页面搜索查询
        if (!StringUtils.isEmpty(brand.getName())){
            qw.eq("name",brand.getName());
        }

        List<Brand> list = bs.list(qw);

        PageInfo<Brand> page = new PageInfo<>(list,brand.getLimit());

        return this.findMap(page.getList(),page.getTotal());
    }

    @Override
    public Map<String, Object> add(Brand brand) {

        boolean b = bs.save(brand);

        return this.iud("新增",b,brand);
    }

    @Override
    public Map<String, Object> update(Brand brand) {

        boolean b = bs.updateById(brand);

        return this.iud("编辑",b,brand);
    }


    @RequestMapping("/batchUpdate")
    @ResponseBody
    public Map<String,Object> batchUpdate(@RequestParam(value = "ids[]") List<Integer> ids){

        Map<String,Object> map = new HashMap<>();

        if (!StringUtils.isEmpty(ids)){
            bs.removeByIds(ids);
            map.put("is",true);
            map.put("msg","删除成功！");
        }else {
            map.put("is",false);
            map.put("msg","删除失败！");
        }

        return map;
    }
}
